import {Component} from "react";

import React from "react";
import axios from "axios"
import './mediamanage.css'
import {NavLink, Redirect, Route} from "react-router-dom";

import Page from "../Page";
// import {nanoid} from "nanoid"
export default class Mediamanage extends Component{
    state = {
        video:{
            cmsPreVideoId:"1",
            videoType:"2",
            videoName:"3",
            outsourceId:"4",
            path:"5"
        },
            videoList:[],
        page:{
            pageNum:"1",
            pageSize:"10",
            prePage:"",
            nextPage:"",
            navigatepageNums:[],
            hasPreviousPage:'',
            hasNextPage:''
        },
        videoType:[]
    }
    videONameRef = React.createRef();
    fileNameRef = React.createRef();
    outsourceIdRef = React.createRef();

    componentDidMount() {
        this.VideoPage();
        axios.request({
            method:"get",
            url: "http://192.168.56.1:7001/scms-edit/video/getVideoType",
        }).then((response)=>{
            this.setState({videoType:response.data.result})
        })
    }
    
    VideoPage = (obj)=>{
        const {page} = this.state;
        // page = obj!=undefined?obj:page;
        console.log(page)
        axios.request({
            method:"post",
            url:"/scms-edit/video/page",
            data:{pageNum:obj!=null?obj:page.pageNum,pageSize:page.pageSize}
        }).then( response => {
            const {data} = response
            this.setState({
                videoList:response.data.result.list,
                page:{
                    pageNum: response.data.result.pageNum,
                    pageSize: response.data.result.pageSize,
                    prePage: response.data.result.prePage,
                    nextPage: response.data.result.nextPage,
                    navigatepageNums: response.data.result.navigatepageNums,
                    hasPreviousPage: response.data.result.hasPreviousPage,
                    hasNextPage: response.data.result.hasNextPage
                }
            })

        },() => {
            console.log("获取分页内容失败")
        })
    }
    toOnePage = (obj)=>{
        // this.setState({page:{
        //     pageNum:obj
        // }})
        this.VideoPage(obj);
    }

    deleteVedio = (video)=>{
        console.log("video = ")
        console.log(video)
        axios.request({
            method: "post",
            url: "http://192.168.56.1:7001/scms-edit/video/deleteVideo",
            data:{
                'cmsPreVideoId' : video.cmsPreVideoId
            }
        }).then(response => {
            this.VideoPage()
        })
    }

    prePage = ()=>{
        if(this.state.page.hasPreviousPage){
            this.VideoPage(this.state.page.prePage)
        }
    }
    nextPage = ()=>{
        if(this.state.page.hasNextPage){
            this.VideoPage(this.state.page.nextPage)
        }
    }

    queryVideoES = ()=>{
        axios.request({
            method: "post",
            data: {
                "videoName": this.videONameRef.current.value,
                "fileId": this.fileNameRef.current.value,
                "videoType": this.state.selectValue,
                "outsourceId": this.outsourceIdRef.current.value,
            },
            url: "http://192.168.56.1:7001/scms-edit/es/queryVideo"
        }).then(response =>{
            console.log(response.data.result)
            this.setState({
                videoList:response.data.result
            })
        })
    }
    clearInput = ()=>{
        this.videONameRef.current.value = ""
        this.fileNameRef.current.value = ""
        this.outsourceIdRef.current.value = ""
        this.setState({selectValue:""})
    }
    getSelectVale = (event)=>{
        this.setState({selectValue:event.target.value})
    }

    componentDidMount() {
        this.VideoPage();
    }

    VideoPage = ()=>{
        const {page} = this.state;
        console.log(page)
        axios.request({
            method:"post",
            url:"/scms-edit/login",
            data:{pageNum:page.pageNum,pageSize:page.pageSize}
        }).then( response => {
            const {data} = response
            this.setState({
                videoList:response.data.result.list
            })
        },reason => {
            console.log("获取分页内容失败")
        })
    }



    render(){
        return(
            <div className="main_content ">
                <span className="title">
                媒资管理
                </span>
                <div class="mediamanage_content">
                    <div class="">
                        <form>
                            影片名字&nbsp;&nbsp;<input type="text" className="footer_input" ref={this.videONameRef}/>&nbsp;&nbsp;
                            文件外键&nbsp;&nbsp;<input type="text" className="footer_input" ref={this.outsourceIdRef}/>&nbsp;&nbsp;
                            文件别名&nbsp;&nbsp;<input type="text" className="footer_input" ref={this.fileNameRef}/>&nbsp;&nbsp;
                            <span className="">
                            产品类型&nbsp;&nbsp;
                                <select className="footer_select" value={this.state.selectValue} onChange={(e)=>{
                                        this.getSelectVale(e)
                                    }}>
                                        <option index="">全部</option>
                                        {
                                            this.state.videoType.map((obj)=>{
                                                return(
                                                    <option index={obj.value}>{obj.desc}</option>
                                                )
                                            })
                                        }
                                </select>
                            </span>&nbsp;&nbsp;
                            <button type="button" className="footer_btn_red" onClick={this.queryVideoES}>查询</button>&nbsp;&nbsp;&nbsp;&nbsp;
                            <button type="button" className="footer_btn_white" onClick={()=>{this.clearInput()}}>重置</button>
                            产品名称&nbsp;&nbsp;<input type="text" className="footer_input" ref={this.checkRef}/>&nbsp;&nbsp;
                            <span className="">
                            产品类型&nbsp;&nbsp;
                                <select className="footer_select">
                                    <option value="222">测试</option>
                                    <option value="222">测试</option>
                                    <option value="222">测试</option>
                                    <option value="222">测试</option>
                                </select>
                            </span>&nbsp;&nbsp;
                            <button type="button" className="footer_btn_red" onClick={this.inquire}>查询</button>&nbsp;&nbsp;&nbsp;&nbsp;
                            <button type="button" className="footer_btn_white">重置</button>
                        </form>
                        <div className="footer_tag">
                            <span>产品状态</span>
                            &nbsp;&nbsp;&nbsp;
                            <span>
                                <input type="checkbox" id="state1" className="tag_input" />
                                <label htmlFor="state1" className="tag_label">销售中</label>
                            </span>
                            <span>
                                <input type="checkbox" id="state2" className="tag_input"/>
                                <label htmlFor="state2" className="tag_label">停售中</label>
                            </span>
                            <span>
                                <input type="checkbox" id="state3" className="tag_input"/>
                                <label htmlFor="state3" className="tag_label">促销中</label>
                            </span>
                        </div>
                        <button className="refresh_btn" onClick={()=>{this.pageRequest(this.state.pages.page)}}>刷新产品列表</button>
                        <div className="total">
                            <span className="glyphicon glyphicon-search total_icon"></span>
                            &nbsp;&nbsp;<span>产品总数 0 件</span>
                        </div>
                        <br></br>
                    </div>

                    <div class = "mediamanage_table_content">
                        <table class="table table-hover table-striped mediamanage_table_text">
                            <tbody class="mediamanage_table_content">
                                <tr class="info">
                                    <td class="mediamanage_table_first_td">影片名字</td>
                                    <td class="mediamanage_table_first_td">文件外键</td>
                                    <td class="mediamanage_table_first_td">文件别名</td>
                                    <td class="mediamanage_table_first_td">路径</td>
                                    <td class="mediamanage_table_first_td">影片类型</td>
                                    <td class="mediamanage_table_first_td">操作</td>
                                </tr>
                                {
                                    this.state.videoList.map((video)=>{
                                        return(
                                            <tr>
                                                <td>{video.videoName}</td>
                                                <td>{video.outsourceId}</td>
                                                <td>{video.fileId}</td>
                                                <td>{video.path}</td>
                                                <td>{video.videoType}</td>
                                                <td>
                                                    <span className="glyphicon glyphicon-remove mediamanage_span_icon" onClick={()=>{this.deleteVedio(video)}}></span>
                                                    <span class="glyphicon glyphicon-open mediamanage_span_icon" ></span>
                                                    <NavLink to={{pathname:"/mediadetails",state:video.fileId}} class="" id="navlink">
                                                        <span class="glyphicon glyphicon-tasks mediamanage_span_icon" ></span>
                                                    </NavLink>
                                                </td>
                                            </tr>
                                        )
                                        
                                    })
                                }
                            </tbody>
                        </table>
                        <div class="mediamanage_page ">
                        <nav aria-label="Page navigation">
                            <ul class="pagination ">
                            <li>
                                <a href="javascript:;" aria-label="Previous" onClick={()=>{
                                    this.prePage()
                                }}>
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            {
                                this.state.page.navigatepageNums.map((obj)=>{
                                    return(
                                        <li><a href="javascript:;" onClick={()=>{
                                            this.toOnePage(obj)
                                        }}>{obj}</a></li>
                                    )

                                })
                            }

                            {/* <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">3</a></li>
                            <li><a href="#">4</a></li>
                            <li><a href="#">5</a></li> */}
                            <li>
                                <a href="javascript:;" aria-label="Next" onClick={()=>{
                                    this.nextPage()
                                }}>
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                            </ul>
                        </nav>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
        
    
}